package com.wwp.tbshop.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.category.GoodsListActivity;
import com.wwp.tbshop.model.bean.CategoryInfo;
import com.wwp.tbshop.model.bean.CategoryTempBean;
import com.wwp.tbshop.util.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 楠GG on 2017/6/15.
 */

public class CategoryBaseRvAdapter extends RecyclerView.Adapter {
    protected Context mContext;
    protected List<CategoryInfo> mDatas = new ArrayList<>();

    public static final int TYPE_TITLE = 0;
    public static final int TYPE_NORMAL = 1;

    public CategoryBaseRvAdapter(Context context) {
        mContext = context;
    }

    public void setDatas(List<CategoryInfo> datas) {
        mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        CategoryInfo info = mDatas.get(position);
        int type = info.type;
        if (type == CategoryTempBean.TYPE_TITLE) {
            return TYPE_TITLE;
        } else {
            return TYPE_NORMAL;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            View titleView = LayoutInflater.from(mContext).inflate(R.layout.item_category_title, parent, false);
            CategoryTitleHolder titleHolder = new CategoryTitleHolder(titleView);
            return titleHolder;
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_category, parent, false);
            CategoryHolder categoryHolder = new CategoryHolder(itemView);
            return categoryHolder;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        CategoryInfo info = mDatas.get(position);
        if(info.type == CategoryInfo.TYPE_TITLE) {
            CategoryTitleHolder titleHolder = (CategoryTitleHolder) holder;
            titleHolder.setData(mDatas.get(position));
        }else {
            CategoryHolder categoryHolder = (CategoryHolder) holder;
            categoryHolder.setData(mDatas.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    class CategoryTitleHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_category_title)
        TextView mItemCategoryTitle;

        CategoryTitleHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public void setData(CategoryInfo info) {
            mItemCategoryTitle.setText(info.title);
        }
    }

    class CategoryHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_category_icon_1)
        ImageView mItemCategoryIcon1;
        @Bind(R.id.item_category_name_1)
        TextView mItemCategoryName1;
        @Bind(R.id.item_category_item_1)
        LinearLayout mItemCategoryItem1;
        @Bind(R.id.item_category_icon_2)
        ImageView mItemCategoryIcon2;
        @Bind(R.id.item_category_name_2)
        TextView mItemCategoryName2;
        @Bind(R.id.item_category_item_2)
        LinearLayout mItemCategoryItem2;
        @Bind(R.id.item_category_icon_3)
        ImageView mItemCategoryIcon3;
        @Bind(R.id.item_category_name_3)
        TextView mItemCategoryName3;
        @Bind(R.id.item_category_item_3)
        LinearLayout mItemCategoryItem3;
        private CategoryInfo mInfo;

        CategoryHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        @OnClick({R.id.item_category_item_1, R.id.item_category_item_2 ,R.id.item_category_item_3})
        public void onViewClicked(View view) {
            switch (view.getId()) {
                case R.id.item_category_item_1:
                    Intent intent = new Intent(mContext, GoodsListActivity.class);
                    intent.putExtra("cId", mInfo.id1);
                    intent.putExtra("name", mInfo.name1);
                    mContext.startActivity(intent);
                    break;
                case R.id.item_category_item_2:
                    Intent intent2 = new Intent(mContext, GoodsListActivity.class);
                    intent2.putExtra("cId", mInfo.id2);
                    intent2.putExtra("name", mInfo.name2);
                    mContext.startActivity(intent2);
                    break;
                case R.id.item_category_item_3:
                    Intent intent3 = new Intent(mContext, GoodsListActivity.class);
                    intent3.putExtra("cId", mInfo.id3);
                    intent3.putExtra("name", mInfo.name3);
                    mContext.startActivity(intent3);
                    break;
            }
        }

        public void setData(CategoryInfo info) {
            mInfo = info;
            mItemCategoryName1.setText(info.name1);
            mItemCategoryName2.setText(info.name2);
            mItemCategoryName3.setText(info.name3);

            String str = Constant.HOST.substring(0, Constant.HOST.length() - 1);
            Picasso.with(mContext).load(str + info.url1)
                    .into(mItemCategoryIcon1);
            Picasso.with(mContext).load(str + info.url2)
                    .into(mItemCategoryIcon2);
            Picasso.with(mContext).load(str + info.url3)
                    .into(mItemCategoryIcon3);
        }
    }
}
